  $(function() {
	  function showError(str,delay){	
		  if(delay){
			  $('#alertMessage').removeClass('success info warning').addClass('error').html(str).stop(true,true).show().animate({ opacity: 1,right: '10'}, 500,function(){
					  $(this).delay(delay).animate({ opacity: 0,right: '-20'}, 500,function(){ $(this).hide(); });																														   																											
				});
			  return false;
		  }
			  	$('#alertMessage').addClass('error').html(str).stop(true,true).show().animate({ opacity: 1,right: '10'}, 500);	
	  }
	  function showSuccess(str,delay){
		  if(delay){
			  $('#alertMessage').removeClass('error info warning').addClass('success').html(str).stop(true,true).show().animate({ opacity: 1,right: '10'}, 500,function(){
					  $(this).delay(delay).animate({ opacity: 0,right: '-20'}, 500,function(){ $(this).hide(); });																														   																											
				});
			  return false;
		  }
			  $('#alertMessage').addClass('success').html(str).stop(true,true).show().animate({ opacity: 1,right: '10'}, 500);	
	  }
	  var list_id = '';
	  $(".productTr").live("click", function(){
		$('#listProductTransfer').validationEngine('hide');
  		var id = $(this).attr("id");
  		var idP = $(this).attr("name");
  		var quantity = $(this).find(".quantityProduct").text();
  		var n = list_id.indexOf(id);
  		if (n == -1) {
	    		var dataString = "id=" + id +"&idP=" + idP+"&quantity=" + quantity;
	    		$("#result_choose_product_transfer").html("<img src='../img/loadder/loader_green.gif'/>");
	    		$.ajax({
	    	        type: "GET",
	    	        url: "choose_product_transfer",
	    	        data: dataString,
	    	        dataType: 'html',
	    	        success: function (data) {
	    	        	list_id = list_id + id + ',';
	    	            $("#result_choose_product_transfer").html(data).hide().fadeIn();
	    	            $("#result_choose_product_transfer").attr('id',id+'_product');
			            $("#list_product").append("<div id='result_choose_product_transfer'></div>");
			            $('form#validation').validationEngine(); 
			            $('.numericonly input').autotab_magic().autotab_filter('numeric');
	    	        }
	    	    })
  		}
  		else
		{
  			showError("Đã thêm sản phẩm này",1000)
		}
  	})
  	$(".remove_product").live("click", function(){
		var id = $(this).attr('id').substr(0,$(this).attr('id').length-7);
		list_id = list_id.replace(id + ',','');
		showSuccess('Đã xóa sản phẩm',1000);
		$("#" + id + "_product").remove();
	})
	$("#create_transfer_invoice").live("click", function(){
		var list_quantity = '';
		var list_quantity_tr = '';
		if (list_id == '')
			{
			$('#listProductTransfer').validationEngine('showPrompt', 'Chọn sản phẩm chuyển kho', 'pass');
			showError('Chọn sản phẩm chuyển kho!',1000);
			}
		else
			{
				$('#listProductTransfer').validationEngine('hide');
				if($("#list_product").validationEngine('validate'))
				{
					var list_id_temp = list_id.substr(0,list_id.length-1);
					var array_id = list_id_temp.split(',');
					for (var i=0; i < array_id.length; i++)
					{
						id = array_id[i];
						list_quantity_tr = list_quantity_tr + $("#" + id + '_quantity').val() + ',';
						list_quantity = list_quantity + $("#"+id).find(".quantityProduct").text() + ',';
					}
					var dataString = "list_id=" + list_id + "&list_quantity=" + list_quantity + "&list_quantity_tr=" + list_quantity_tr ;
					$.ajax({
				        type: "GET",
				        url: "create_transfer_invoice",
				        data: dataString,
				        dataType: 'html',
				        success: function (data) {
				        	showSuccess(data,1000);
				            var list_quantity_temp = list_quantity.substr(0,list_quantity.length-1);
							var array_quantity = list_quantity_temp.split(',');
							var list_quantity_tr_temp = list_quantity_tr.substr(0,list_quantity_tr.length-1);
							var array_quantity_tr = list_quantity_tr_temp.split(',');
							for (var i=0; i < array_id.length; i++)
							{
								id = array_id[i];
								$("#"+id).find(".quantityProduct").text(array_quantity[i] - array_quantity_tr[i]);
								$("#" + id + "_product").remove();
							}
							list_id = '';
				        }
				    })
				}
				else
					{
						$("#list_product").validationEngine('validate')
					}
			}	
	})
  })